package jgl.vajra.kingkong.part.home.fragment;

import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.HomeCarModelBean;
import jgl.vajra.kingkong.bean.OrderListBean;
import jgl.vajra.kingkong.bean.OrderParametersBean;
import jgl.vajra.kingkong.event.OrderPageListEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.home.contract.OrderPageListContract;
import jgl.vajra.kingkong.part.home.pop.ModifyPricePop;
import jgl.vajra.kingkong.part.home.viewModel.OrderPageListViewModel;
import jgl.vajra.kingkong.part.vehicle.pop.RewardPop;
import jgl.vajra.kingkong.widget.base.BaseListFragment;
import jgl.vajra.kingkong.widget.bean.BaseListData;
import jgl.vajra.kingkong.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(OrderPageListViewModel.class)
public class OrderPageListFragment extends BaseListFragment<OrderPageListViewModel, String> implements OrderPageListContract.View {

    private String authorization;
    private int type;
    private RewardPop rewardPop;
    private ModifyPricePop modifyPricePop;

    @Override
    protected RefreshRecyclerNetConfig getRefreshRecyclerNetConfig() {
        return new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", authorization);
                map.put("type", type);
                return mViewModel.getOrderList(map);
            }
        };
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_order_page_list;
    }

    @Override
    public void onItemClick(int position, String itemData) {

    }

    @Override
    public void initView() {
        super.initView();
        type = getArguments().getInt("type");

        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<OrderListBean>>() {
            @Override
            public void onSucceed(BaseListData<OrderListBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                for (int i = 0; i < data.data.size(); i++) {
                    data.data.get(i).sizeAddressList = data.data.get(i).addressList.size();
                    String channelText = "";
                    String addressEnd = "";
                    if (data.data.get(i).addressList.size() > 2) {
                        int channelNum = data.data.get(i).addressList.size() - 2;
                        channelText = "经过" + channelNum + "个途径点";
                    }
                    if (data.data.get(i).addressList.size() >= 2) {
                        List<OrderListBean.AddressListBean> addressList = data.data.get(i).addressList;
                        addressEnd = addressList.get(addressList.size() - 1).address;
                    }
                    data.data.get(i).channelText = channelText;
                    data.data.get(i).addressEnd = addressEnd;
                }

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
    }

    @Override
    public void onDetails(OrderListBean itemData) {


        OrderParametersBean orderParameters = new OrderParametersBean();
        orderParameters.priceType = itemData.priceType;

        List<OrderParametersBean.AddressData> addressData = new ArrayList<>();
        OrderParametersBean.AddressData addressDataBean = new OrderParametersBean.AddressData();
        addressDataBean.latitude = itemData.addressList.get(0).latitude;
        addressDataBean.longitude = itemData.addressList.get(0).longitude;
        addressData.add(addressDataBean);
        orderParameters.addressData = addressData;

        if (itemData.status == 1) {
            //等待接单
            IntentManager.toWaitingOrderActivity(mActivity, itemData.orderId, orderParameters, itemData.preparatoryPrice, itemData.amount, true, itemData.orderTime);

        } else if (itemData.status == 2 || itemData.status == 3) {//进行中
            IntentManager.toOrderStatusActivity(mActivity, itemData.orderId, orderParameters, itemData.preparatoryPrice, itemData.amount, true, itemData.orderTime);

        } else if (itemData.status == 4 || itemData.status == 5 || itemData.status == 7) {//4已取消,5已取消(未退款),7已完成
//            ToastUtils.showShort("4已取消,5已取消(未退款),7已完成");
            IntentManager.toOrderDetailsCompleteActivity(mActivity, itemData.orderId);
        }
    }

    @Override
    public void onDetermine(OrderListBean itemData) {
        //加价
        if (itemData.status == 1) {
           /* rewardPop = new RewardPop(mActivity, authorization, orderListBean.amount, orderListBean.orderId);
            rewardPop.setRewardListener(amount -> {
                orderListBean.amount = amount;
                mAdapter.refresh();

            });
            rewardPop.showPopupWindow(mBinding.getRoot());*/

            if (itemData.priceType == 0) {
                rewardPop = new RewardPop(mActivity,itemData.preparatoryPrice, authorization, itemData.amount, itemData.orderId);
                rewardPop.setRewardListener(amount -> {
                    itemData.amount = amount;
                    mAdapter.refresh();
                });
                rewardPop.showPopupWindow(mBinding.getRoot());
            } else {
                modifyPricePop = new ModifyPricePop(mActivity, itemData.preparatoryPrice, authorization, itemData.amount, itemData.orderId);
                modifyPricePop.setModifyPriceListener(amount -> {
                    itemData.amount = amount;
                    mAdapter.refresh();
                });
                modifyPricePop.showPopupWindow(mBinding.getRoot());
            }
        } else if (itemData.status == 2) { //立即支付
            IntentManager.toImmediatePaymentActivity(mActivity, itemData.amount, itemData.orderId,1);
        } else if (itemData.status == 3) {//进行中 订单延时付款
            IntentManager.toImmediatePaymentActivity(mActivity, itemData.overtimeAmount, itemData.overtimeId+"",2);

        } else if (itemData.status == 4 || itemData.status == 5 || itemData.status == 7) {//4已取消,5已取消(未退款),7已完成

            //确认单子
            OrderParametersBean orderParameters = new OrderParametersBean();
            orderParameters.area=itemData.area;
            orderParameters.amount=itemData.amount;
            orderParameters.model=itemData.model;
            orderParameters.hour=itemData.hour;
            orderParameters.minute=itemData.minute;
            orderParameters.workTime=itemData.workTime;
            orderParameters.workContent=itemData.workTimeDescription;

            orderParameters.addressData=new ArrayList<>();
            for (int i = 0; i < itemData.addressList.size(); i++) {
                OrderParametersBean.AddressData  addressData=new OrderParametersBean.AddressData();
                addressData.address=itemData.addressList.get(i).address;
                addressData.latitude=itemData.addressList.get(i).latitude;
                addressData.longitude=itemData.addressList.get(i).longitude;
                addressData.contactPerson=itemData.addressList.get(i).contactPerson;
                addressData.contactPhone=itemData.addressList.get(i).contactPhone;
                addressData.city=itemData.addressList.get(i).city;
                addressData.addressType=Integer.parseInt(itemData.addressList.get(i).addressType);

                orderParameters.addressData.add(addressData);
            }


            HomeCarModelBean homeCarModelBean = new HomeCarModelBean();

            homeCarModelBean.remark=itemData.remark;
            homeCarModelBean.fileKey=itemData.fileKey;

            homeCarModelBean.carParams=itemData.carParams;

            homeCarModelBean.displayNumber=2;
            homeCarModelBean.extraRequireData= itemData.extraRequireData;

            IntentManager.toConfirmOrderActivity(mActivity, orderParameters, homeCarModelBean, "", itemData.preparatoryPrice);
        }
    }

    //评论
    @Override
    public void onComment(String orderId) {
        IntentManager.toOrderEvaluateActivity(mActivity, orderId);
    }

    @Override
    public void onResume() {
        super.onResume();
        mBinding.recyclerView.firstLoad();
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOrderPageListEvent(OrderPageListEvent event) {
        mBinding.recyclerView.firstLoad();
    }
}
